<template>
    <el-form :model="form" label-width="100px" class="lg_form">
        <el-form-item><div class="lg_header">无人机健康管理系统</div></el-form-item>
        <el-form-item label="用户名">
            <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="密码">
            <el-input v-model="form.passwd" show-password/>
        </el-form-item>
        <el-form-item label="记住密码">
            <el-switch v-model="form.remember" @change="remember_pw"/>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onLogin">登录</el-button>
            <el-button @click="onCancel">取消</el-button>
        </el-form-item>
        <el-form-item>
            <span style="margin:auto 0;color:red">{{message}}</span>
        </el-form-item>
    </el-form>
</template>

<script>
import {ref} from 'vue'
export default {
    setup() {
        document.title = '登录'
    },
    data(){
        let pw = localStorage.getItem("pw")
        return {
            form:{
                name:localStorage.getItem("user")||'',
                passwd:pw==null?'':atob(pw),
            },
            message:ref('')
        }
    },
    methods:{
        onLogin(){
            this.message = ''
            document.cookie = 'user='+ this.form.name
            localStorage.setItem('user', this.form.name)
            window.cefQueryRequset("user", "login",this.form,(status,data)=>{
                if(status){
                    this.$emit('onlogin',true)
                    this.$router.replace('/main')
                }else{
                    this.message = data;
                }
            })
            
        },
        onCancel(){
            window.cefQueryRequset("user","cancel",this.form)
        },
        remember_pw(val){
            if(this.form.passwd === ''){
                this.message = '密码为空'
                return
            }
            if(val)
                localStorage.setItem('pw', btoa(this.form.passwd))
            else
                localStorage.removeItem('pw')
        }
    }
}
</script>

<style scoped>
.lg_form{
    margin:60px;
    position:fixed;
    margin:40px 120px 40px 40px;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}
.lg_header{
    display: block;
    font-size: large;
    margin: auto;
}
</style>